﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;
using BLL;

public partial class main : System.Web.UI.Page
{

    SecurityEvaluateDataContext db = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        db = new SecurityEvaluateDataContext();
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            BindProblem(0);
        }

    }

    void BindProblem(int PageIndex)
    {
        
        List<DAL.Examination> examination = BLL.Security.getSubjectByUserID(User.Identity.Name).ToList();
        QuestionDataContext db = new QuestionDataContext();
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        var result2 = Security2.getSubjectByUserID(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
       // var result = (from obj in db.Examination where obj.DepartmentID == DepartmentID select obj).ToList();

        foreach (DAL2.Examination ex in result2)
        {
            examination.Add(new DAL.Examination() { ID = ex.ID, DepartmentID = ex.DepartmentID, EndTime = ex.EndTime, SetUserID = ex.SetUserID, SID = ex.SID, StartTime = ex.StartTime });
        }

        ListPageContral1.ItemCount = examination.Count();
        GV.DataSource = examination.Skip(PageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize);
        GV.DataBind();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindProblem(PageIndex);
    }

    public string GetPageUrl(object obj)
    {
        if (obj == null)
            return "";
        Question2DataContext db1 = new Question2DataContext();
        var result = (from o in db1.Examination2 where o.SID == new Guid(obj.ToString()) select o).FirstOrDefault();
        if (result == null)
            return "Problems2.aspx";
        else
            return "Problems.aspx";

    
    }

    protected void gvChemical_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            Label lblDepartment = e.Row.FindControl("lblDepartment") as Label;
            lblID.Text = Convert.ToString(GV.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            DAL.Examination exam = e.Row.DataItem as DAL.Examination;
            DAL.SecuritySubject subject = BLL.Security.getSubjectByID(exam.SID.Value.ToString());
            Label lblCaption = e.Row.FindControl("lblCaption") as Label;
            if (subject != null)
            {
                lblCaption.Text = subject.Caption;                
                DAL.FireMisDataContext fdb = new FireMisDataContext();
                string departmentid = fdb.Account_Users.Where(o => o.UserID == exam.SetUserID).FirstOrDefault().DepartmentID;
                lblDepartment.Text = BLL.Department.GetDepartmentByID(departmentid).ShortName;
            }
            else
            { 
                QuestionDataContext db1=new QuestionDataContext();
               
               
                SecuritySubject sub2=(from obj in db1.SecuritySubject where obj.Id==new Guid( GV.DataKeys[e.Row.RowIndex].Value.ToString()) select obj ).FirstOrDefault();
                DAL.FireMisDataContext fdb = new FireMisDataContext();
                string departmentid = fdb.Account_Users.Where(o => o.UserID == exam.SetUserID).FirstOrDefault().DepartmentID;
                lblDepartment.Text = BLL.Department.GetDepartmentByID(departmentid).ShortName;
                if (sub2 != null)
                {
                    lblCaption.Text = sub2.Caption;
                }
            }
           
        }
    }
}
